package com.livecooking;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class FoodsDataSource {

	// Database fields
	private SQLiteDatabase database;
	private DataBaseHelp dbHelper;
	private String[] columnList = { DataBaseHelp.COLUMN_FOOD_NAME,
			DataBaseHelp.COLUMN_FOOD_ID, DataBaseHelp.COLUMN_FOOD_BOOKMARK };

	private String[] columnHowToMake = { DataBaseHelp.COLUMN_FOODDETAILS_HOWTOMAKE };

	// private String[] ColumnsImage = { DataBaseHelp.COLUMN_F_IMAGE };

	public FoodsDataSource(Context context) {
		dbHelper = new DataBaseHelp(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public List<Food> getAllFood() {
		List<Food> listFood = new ArrayList<Food>();
		Cursor cursor = database.query(DataBaseHelp.TABLE_FOOD, columnList,
				null, null, null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Food food = cursorToComment(cursor);
			listFood.add(food);
			cursor.moveToNext();
		}
		cursor.close();
		return listFood;
	}

	public String getHowToMake(String foodName) {
		String howToMake;

		Cursor cursor = database.query(DataBaseHelp.TABLE_FOODDETAILS,
				columnHowToMake, DataBaseHelp.COLUMN_FOOD_NAME + " = '"
						+ foodName + "'", null, null, null, null);
		cursor.moveToFirst();
		howToMake = cursor.getString(0);
		cursor.close();

		return howToMake;
	}

	public List<Food> getFoodType(String foodType) {
		List<Food> listFood = new ArrayList<Food>();
		Cursor cursor = database.query(DataBaseHelp.TABLE_FOOD, columnList,
				DataBaseHelp.COLUMN_FOOD_TYPE + " = '" + foodType + "'", null,
				null, null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Food food = cursorToComment(cursor);
			listFood.add(food);
			cursor.moveToNext();
		}
		cursor.close();
		return listFood;
	}

	public List<Food> getAllBookmark() {
		List<Food> listFood = new ArrayList<Food>();
		Cursor cursor = database.query(DataBaseHelp.TABLE_FOOD, columnList,
				DataBaseHelp.COLUMN_FOOD_BOOKMARK + " = " + 1, null, null,
				null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Food food = cursorToComment(cursor);
			listFood.add(food);
			cursor.moveToNext();
		}
		cursor.close();
		return listFood;
	}
	
	public List<Integer> getBookmarkList(){
		List<Integer> listBookmark = new ArrayList<Integer>();
		Cursor cursor = database.query(DataBaseHelp.TABLE_FOOD, columnList,
				null, null, null,
				null, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Integer bookmark=cursor.getInt(2);
			listBookmark.add(bookmark);
			cursor.moveToNext();
		}
		cursor.close();
		return listBookmark;
	}

	public void update(Integer bookmark, Integer id) {
		database.execSQL("UPDATE " + DataBaseHelp.TABLE_FOOD + " SET "+DataBaseHelp.COLUMN_FOOD_BOOKMARK+"  = " + bookmark
				+ " WHERE "+DataBaseHelp.COLUMN_FOOD_ID+" = " + id + "");

	}

	private Food cursorToComment(Cursor cursor) {
		Food food = new Food();
		food.setName(cursor.getString(0));
		food.setId(cursor.getInt(1));
		food.setBookMark(cursor.getInt(2));
		return food;
	}
}
